package network;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.TimerTask;

import org.apache.log4j.Logger;

import core.Core;

public class KeepAliveTimerTask extends TimerTask {

	private Socket socket ;
	private Logger warningsLog=Core.warningsLog;
	
	public KeepAliveTimerTask(Socket socket){
		this.socket=socket;
	}
	
	@Override
	/**
	 * Cette fonction envoie un message KeepAlive au peer
	 */
	public void run() {				
		try {
			OutputStream output = socket.getOutputStream();		
			//on envoie notre keepAlive
			output.write(MessageFactory.createKeepAlive());
			output.flush();
			
		} catch (IOException e) {
			warningsLog.warn("> Connection.run() : Erreur d'ecriture sur la socket du peer "+ socket.getInetAddress().getHostAddress() +":"+ socket.getPort() +".\n" + e.getMessage());
		}
	}

}
